package com.extrace.loader;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.extrace.misc.model.ExpressSheet;
import com.extrace.net.HttpAsyncTask;
import com.extrace.net.HttpResponseParam.RETURN_STATUS;
import com.extrace.net.IDataAdapter;
import com.extrace.net.JsonUtils;
import com.extrace.ui.main.ExTraceApplication;
import com.google.gson.reflect.TypeToken;

import java.util.List;

public class ExpressListLoader extends HttpAsyncTask {

    String url;
    IDataAdapter<List<ExpressSheet>> adapter;
    private Activity context;

    public ExpressListLoader(IDataAdapter<List<ExpressSheet>> adpt, Activity context) {
        super(context);
        this.context = context;
        adapter = adpt;
        url = ((ExTraceApplication)context.getApplication()).getDomainServiceUrl();
    }

    @Override
    public void onDataReceive(String class_data, String json_data) {
        if(json_data.equals("Deleted")){
            //adapter.getData().remove(0);	//这个地方不好处理
            Toast.makeText(context, "快件信息已删除!", Toast.LENGTH_SHORT).show();
        }
        else if (json_data.equals("M_EFP")){

        }
        else {
            List<ExpressSheet> es = JsonUtils.fromJson(json_data, new TypeToken<List<ExpressSheet>>(){});
            adapter.setData(es);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onStatusNotify(RETURN_STATUS status, String str_response) {
        Log.i("onStatusNotify", "onStatusNotify: " + str_response);
    }

    public void LoadExpressListInPackage(String pkgId) {
        url += "getExpressListInPackage/PackageId/"+pkgId+"?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void LoadExpressListInPackageByStatus(int status) {
        int uid = ((ExTraceApplication) context.getApplication()).getLoginUser().getUID();
        url += "getExpressListInPackageByStatus/uid/"+uid+"/status/" + status + "?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void LoadExpressSheetByStatus(int status) {
        int uid = ((ExTraceApplication) context.getApplication()).getLoginUser().getUID();
        url += "getExpressSheetByStatus/" + status + "/uid/"+ uid +"?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void LoadExpressSheet(String id) {
        url += "getExpressSheet/" + id + "?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

